// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    let godsMes = await db.collection('gods').where({
        _openid: wxContext.OPENID,
    }).get()

    let arr = []
    if (event.list != undefined) {  //删除
        for (const index in event.list) {
            if (event.list[index].type == "card") {
                await db.collection('card').doc(event.list[index]._id).remove()
                let newList = []
                for (const key in godsMes.data[0].publish.card) {
                    if (godsMes.data[0].publish.card[key]._id != event.list[index]._id) {
                        newList.push(godsMes.data[0].publish.card[key])
                    }
                }
                await db.collection('gods').where({
                    _openid: wxContext.OPENID,
                }).update({
                    data: {
                        'publish.card': newList
                    }
                })
            } else if (event.list[index].type == "usedGoods") {
                await db.collection('usedGoods_list').doc(event.list[index]._id).remove()
                let newList = []
                let delList = []
                for (const key in godsMes.data[0].publish.usedGoods) {
                    if (godsMes.data[0].publish.usedGoods[key]._id != event.list[index]._id) {
                        newList.push(godsMes.data[0].publish.usedGoods[key])
                    } else {
                        godsMes.data[0].publish.usedGoods[key].images.forEach(src => {
                            delList.push(src)
                        });

                    }
                }
                await db.collection('gods').where({
                    _openid: wxContext.OPENID,
                }).update({
                    data: {
                        'publish.usedGoods': newList
                    }
                })
                await cloud.deleteFile({
                    fileList: delList,
                })
            } else if (event.list[index].type == "lostGoods") {
                await db.collection('lostGoods_list').doc(event.list[index]._id).remove()
                let newList = []
                let delList = []
                for (const key in godsMes.data[0].publish.lostGoods) {
                    if (godsMes.data[0].publish.lostGoods[key]._id != event.list[index]._id) {
                        newList.push(godsMes.data[0].publish.lostGoods[key])
                    } else {
                        godsMes.data[0].publish.lostGoods[key].images.forEach(src => {
                            delList.push(src)
                        });

                    }
                }
                await db.collection('gods').where({
                    _openid: wxContext.OPENID,
                }).update({
                    data: {
                        'publish.lostGoods': newList
                    }
                })
                await cloud.deleteFile({
                    fileList: delList,
                })
            } else if (event.list[index].type == "money") {
                await db.collection('money_list').doc(event.list[index]._id).remove()
                let newList = []
                let delList = []

                for (const key in godsMes.data[0].publish.money) {
                    if (godsMes.data[0].publish.money[key]._id != event.list[index]._id) {
                        newList.push(godsMes.data[0].publish.money[key])
                    } else {
                        godsMes.data[0].publish.lostGoods[key].images.forEach(src => {
                            delList.push(src)
                        });
                    }
                }
                await db.collection('gods').where({
                    _openid: wxContext.OPENID,
                }).update({
                    data: {
                        'publish.money': newList
                    }
                })
                await cloud.deleteFile({
                    fileList: delList,
                })
            } else {
                return "404"
            }
        }

        return "ok"
    } else {  //请求

        let data = []

        for (const key in godsMes.data[0].publish) {
            data.push({
                types: key,
                list: godsMes.data[0].publish[key]
            })
        }

        return data

    }
}